<template>
    <div>
        <!-- 条件列表 -->
        <div style="text-align:left" v-if="flg.searchFlg">
            <el-form :inline="true" :model="queryCondition" class="demo-form-inline">
                <el-form-item label="系统模块" style="font-weight: bold">
                    <el-input v-model="queryCondition.title" placeholder="请输入系统模块" size="mini"></el-input>
                </el-form-item>
                <el-form-item label="操作人员" style="font-weight: bold">
                    <el-input v-model="queryCondition.operName" placeholder="操作人员" size="mini"></el-input>
                </el-form-item>

                <el-form-item label="类型" style="font-weight: bold">
                    <el-select v-model="queryCondition.businessType" clearable prop="isDelete" size="mini">
                        <el-option label="新增" value="1"></el-option>
                        <el-option label="修改" value="2"></el-option>
                        <el-option label="重置" value="3"></el-option>
                        <el-option label="删除" value="4"></el-option>
                        <el-option label="授权" value="5"></el-option>
                        <el-option label="导出" value="6"></el-option>
                        <el-option label="导入" value="7"></el-option>
                        <el-option label="生成代码" value="8"></el-option>
                        <el-option label="清空数据" value="9"></el-option>
                        <el-option label="冻结" value="10"></el-option>
                        <el-option label="其它" value="0"></el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="操作时间" style="font-weight: bold">
                    <el-date-picker v-model="rangeTime" type="daterange" size="mini" range-separator="至"
                        start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy-MM-dd HH:mm:ss"
                        :default-time="['00:00:00', '23:59:59']">
                    </el-date-picker>
                </el-form-item>

                <el-form-item label="状态" style="font-weight: bold">
                    <el-select v-model="queryCondition.status" clearable prop="isDelete" size="mini">
                        <el-option label="成功" value="0"></el-option>
                        <el-option label="失败" value="1"></el-option>
                    </el-select>
                </el-form-item>

                <el-form-item>
                    <el-button type="primary" size="mini" @click="getList()">查询</el-button>
                </el-form-item>
            </el-form>
        </div>

        <!-- 操作列表 -->
        <el-row style="margin-bottom:10px">
            <el-col :span="12" style="text-align:left">
                <el-button type="danger" icon="el-icon-delete" size="mini" plain @click="handleDelete">删除</el-button>
                <el-button type="danger" icon="el-icon-minus" size="mini" plain @click="handleclear">清空</el-button>

                <el-button type="danger" icon="el-icon-minus" size="mini" plain @click="handleCacheclear">清空缓存</el-button>
            </el-col>
            <el-col :span="12" style="text-align:right">
                <el-tooltip class="item" effect="dark" :content="flg.searchFlg ? '隐藏搜索' : '展开搜索'" placement="top">
                    <el-button icon="el-icon-search" size="mini" circle @click="flg.searchFlg = !flg.searchFlg"></el-button>
                </el-tooltip>
                <el-tooltip class="item" effect="dark" content="刷新" placement="top">
                    <el-button icon="el-icon-refresh" size="mini" circle @click="getList"></el-button>
                </el-tooltip>
            </el-col>
        </el-row>
        <!-- 操作列表 -->

        <!-- 条件列表 -->
        <el-table :data="list" stripe style="width: 100%" @selection-change="handleSelectionChange"
            v-loading="flg.loadingFlg">
            <el-table-column type="selection" width="45" />
            <el-table-column label="日志编号" prop="operId" />
            <el-table-column label="系统模块" prop="title" :show-overflow-tooltip="true" />
            <el-table-column label="操作类型" prop="businessType">
                <template slot-scope="scope">
                    <el-tag v-show="scope.row.businessType == 1" type="success">新增</el-tag>
                    <el-tag v-show="scope.row.businessType == 2" type="warning">修改</el-tag>
                    <el-tag v-show="scope.row.businessType == 3" type="warning">重置</el-tag>
                    <el-tag v-show="scope.row.businessType == 4" type="danger">删除</el-tag>
                    <el-tag v-show="scope.row.businessType == 5" type="success">授权</el-tag>
                    <el-tag v-show="scope.row.businessType == 6" type="success">导出</el-tag>
                    <el-tag v-show="scope.row.businessType == 7" type="success">导入</el-tag>
                    <el-tag v-show="scope.row.businessType == 8" type="warning">生成代码</el-tag>
                    <el-tag v-show="scope.row.businessType == 9" type="warning">清空数据</el-tag>
                    <el-tag v-show="scope.row.businessType == 10" type="danger">冻结</el-tag>
                    <el-tag v-show="scope.row.businessType == 0" type="info">其他</el-tag>
                </template>
            </el-table-column>
            <el-table-column label="操作人员" align="center" prop="operName" :show-overflow-tooltip="true" />
            <el-table-column label="操作地址" align="center" prop="operIp" :show-overflow-tooltip="true" />
            <el-table-column label="操作地点" align="center" prop="operLocation" :show-overflow-tooltip="true" />
            <el-table-column label="操作类型" prop="businessType">
                <template slot-scope="scope">
                    <el-tag v-show="scope.row.status == 0" type="success">成功</el-tag>
                    <el-tag v-show="scope.row.status == 1" type="danger">失败</el-tag>
                </template>
            </el-table-column>

            <el-table-column label="操作日期" prop="operTime">
                <template slot-scope="scope">
                    <span>{{ formatDate(scope.row.operTime) }}</span>
                </template>
            </el-table-column>
            <el-table-column label="消耗时间" prop="costTime" :show-overflow-tooltip="true">
                <template slot-scope="scope">
                    <span>{{ scope.row.costTime }}毫秒</span>
                </template>
            </el-table-column>
            <el-table-column label="操作" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" icon="el-icon-view"
                        @click="handleView(scope.row, scope.index)">详细</el-button>
                </template>
            </el-table-column>
        </el-table>
        <!--    分页框-->
        <div class="block">
            <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                :current-page="queryCondition.pageSize" :page-sizes="[10, 20, 50, 100, 10000]"
                :page-size="queryCondition.pageNum" layout="total, sizes, prev, pager, next, jumper"
                :total="queryCondition.pageTotal">
            </el-pagination>
        </div>
        <!--    分页框-->

        <!-- 操作日志详细 -->
        <el-dialog title="操作日志详细" :visible.sync="dialog.dialogFormVisible" width="700px" :append-to-body="true">
            <el-form ref="form" :model="form" label-width="100px" size="mini">
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="操作模块：">{{ form.title }} </el-form-item>
                        <el-form-item label="登录信息：">{{ form.operName }} / {{ form.operIp }} / {{ form.operLocation
                        }}</el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="请求地址：">{{ form.operUrl }}</el-form-item>
                        <el-form-item label="请求方式：">{{ form.requestMethod }}</el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="操作方法：">{{ form.method }}</el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="请求参数：">{{ form.operParam }}</el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="返回参数：">{{ form.jsonResult }}</el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="操作状态：">
                            <div v-if="form.status === 0">正常</div>
                            <div v-else-if="form.status === 1">失败</div>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="消耗时间：">{{ form.costTime }}毫秒</el-form-item>
                    </el-col>
                    <el-col :span="10">
                        <el-form-item label="操作时间：">{{ formatDate(form.operTime) }}</el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="异常信息：" v-if="form.status === 1">{{ form.errorMsg }}</el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialog.dialogFormVisible = false">关 闭</el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script>
import { list, deleteOperLog, clear, clearCache } from "@/js/api/monitor/operlog"
import alter from '@/js/axios'
export default {
    name: "operlogIndex",
    data() {
        return {
            list: [],
            multipleSelectionId: [],//多选的id
            queryCondition: {
                pageSize: 1,//查询第几页
                pageNum: 10,//一页显示多少行
                title: "",
                operName: "",
                businessType: "",
                status: "",
                srartTime: "",
                endTime: ""
            },
            rangeTime: [],
            flg: {
                loadingFlg: true,
                searchFlg: true,
                batchDeleteFlg: true
            },
            dialog: {
                dialogFormVisible: false,
            },
            form: {}
        }
    },
    created() {
        //加载用户列表
        this.getList()
    },
    methods: {
        handleSizeChange(val) {
            //  修改一页显示条数
            this.queryCondition.pageNum = val
            this.getList()
        },
        handleCurrentChange(val) {
            //修改页数
            this.queryCondition.pageSize = val
            this.getList()
        },
        //处理多选
        handleSelectionChange(val) {
            this.multipleSelectionId = []
            val.forEach(element => {
                this.multipleSelectionId.push(element.operId)
            });
        },
        //回写数据到页面
        handleView(row) {
            this.dialog.dialogFormVisible = true
            this.form = row
        },
        //清空日志
        handleclear() {
            this.$confirm('是否清空日志?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.flg.loadingFlg = true
                clear().then(() => {
                    this.getList()
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消'
                });
            });
        },
        //删除数据
        handleDelete() {
            this.$confirm('是否删除数据?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                if (this.multipleSelectionId.length == 0) {
                    alter.errorMessage("未选择数据！")
                    return
                }
                let params = {
                    ids: this.multipleSelectionId.toString()
                }
                this.flg.loadingFlg = true
                deleteOperLog(params).then(() => {
                    this.getList()
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消'
                });
            });
        },
        //清空日志缓存
        handleCacheclear() {
            clearCache().then(()=>{
                this.getList()
            })
        },
        //获取日志列表信息
        getList() {
            this.flg.loadingFlg = true

            let params = {
                pageSize: this.queryCondition.pageSize,//查询第几页
                pageNum: this.queryCondition.pageNum,//一页显示多少行
                title: this.queryCondition.title,
                operName: this.queryCondition.operName,
                businessType: this.queryCondition.businessType,
                status: this.queryCondition.status,
                startTime: this.rangeTime[0] == undefined ? null : this.rangeTime[0],
                endTime: this.rangeTime[1] == undefined ? null : this.rangeTime[1],
            }

            list(params).then(res => {
                this.list = res.data.data.list
                this.queryCondition.pageTotal = res.data.data.size
                this.flg.loadingFlg = false
            })

        },

    }
}
</script>
<style></style>